<?php

namespace App\Services;


use App\Models\FleaCollectionGoods;
use App\Models\Mini\FleaGoodsModel;

class FleaGoodsService extends BaseService
{
    protected $user;

    public function __construct()
    {
        $this->user = auth('user')->user();
    }

    public function getList($where = null, $order = 'id', $sort = 'desc', $field = null, $limit = 15)
    {
        $user = $this->user;
        $model = FleaGoodsModel::where($where);
        if (isset($order)) {
            $model = $model->orderBy($order, $sort);
        }
        return $model->select()->paginate($limit)->toArray();
    }

    /**
     * @Notes 发布闲置
     * @param $data
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:11
     */
    public function addFleaGoods($data): array
    {
        $data['user_id'] = $this->user['id'];
        $data['community_id'] = $this->user['community_id'];
        $data['estate_id'] = $this->user['property_id'];
        $data['created_at'] = time();
        $result = FleaGoodsModel::insert($data);
        return $result ? $this->format($result) : $this->format_error($result);
    }

    /**
     * 获取用户发布的闲置列表
     * @Notes
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:13
     */
    public function getFealGoodsListByUserId(): array
    {
        $result = FleaGoodsModel::with('user_info')
            ->where('user_id', $this->user['id'])
            ->where('is_sale', '!=', 5)
            ->orderBy('is_sale', 'asc')
            ->select()
            ->paginate(request()->per_page ?? 10)
            ->toArray();
        return $result ? $this->format($result) : $this->format_error('暂无数据');
    }

    /**
     * @Notes 编辑
     * @param $data
     * @return mixed
     * @author ypp
     * @date 2021/3/6
     * @time 11:42
     */
    public function edit($data)
    {
        $data['updated_at'] = time();
        $result = FleaGoodsModel::where(['id' => $data['id']])->update($data);
        return $result ? $this->format([]) : $this->format_error('操作失败');
    }

    /**
     * @Notes 卖出操作
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/8
     * @time 11:15
     */
    public function saleGoods($id): array
    {
        $info = FleaGoodsModel::where(['id' => $id, 'user_id' => $this->user['id']])->first();
        if (!$info) {
            return $this->format_error('数据不存在');
        }
        $info->is_sale = 1;
        $info->updated_at = time();
        $result = $info->save();

        return $result ? $this->format([]) : $this->format_error('操作失败');
    }

    /**
     * @Notes 闲置商品详情
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/6
     * @time 11:14
     */
    public function detail($id, $flag = 1): array
    {
        $where['id'] = $id;
        if ($flag == 1) {
            $where['user_id'] = $this->user['id'];
        }
        $info = FleaGoodsModel::with('user_info')->where($where)->first();
        //获取收藏状态
        $collect = false;
        //获取用户已经点赞过的商品
        if($info){
            $data = FleaCollectionGoods::query()->where('user_id', $this->user['id'])->value('goods_ids') ?? '[]';
            $goodsId = json_decode($data, true);
            if (in_array($id, $goodsId)) {
                $collect = true;
            }
            $info->is_collect = $collect;
        }
        return $info ? $this->format($info) : $this->format_error('数据不存在');
    }

    /**
     * @Notes 删除
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/9
     * @time 10:38
     */
    public function delete($id)
    {
        $result = FleaGoodsModel::where(['id' => $id])->update(['status' => -1, 'is_sale' => 5]);
        return $result ? $this->format([]) : $this->format_error('删除失败');
    }

    /**
     * @Notes 删除
     * @param $id
     * @return array
     * @author ypp
     * @date 2021/3/9
     * @time 10:38
     */
    public function pcDelete($id, $note, $is_sale)
    {
        $data['status'] = '-1';
        $data['refuse_info'] = $note;
        $data['is_sale'] = $is_sale;
        $result = FleaGoodsModel::where(['id' => $id])->update($data);
        return $result ? $this->format([]) : $this->format_error('删除失败');
    }

    /**
     * @Notes 根据type获取闲置商品列表  type --1:分类商品  2：推荐商品
     * @return array
     * @author ypp
     * @date 2021/3/16
     * @time 14:33
     */
    public function getFleaGoodsByType(): array
    {
        $model = FleaGoodsModel::query()->where(['is_sale'=>0,'status'=>1]);
        $type = request()->type;
        $cate_id = request()->cate_id;
        $keywords = request()->keywords;
        if($cate_id){
            $model->where(['cate_id'=>$cate_id]);
        }
        if($keywords){
            $model->where('goods_name', 'like', '%'.$keywords.'%');
        }
        switch ($type){
            case 1:
                $order = 'created_at';
                break;
            case 2:
                $order = 'goods_collect';
                break;

        }
        $result = $model
            ->select('id','cate_id','goods_name','goods_images','goods_price')
            ->paginate(request()->per_page ?? 10)
            ->toArray();
        return $result ? $this->format($result) : $this->format_error('暂无数据');
    }


}
